<?php
/***************************************************************
 *  Copyright notice
 *  (c) 2011 Georg Ringer <typo3@ringerge.org>
 *  All rights reserved
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/

/**
 * Service for category related stuff
 *
 * @package TYPO3
 * @subpackage tx_news
 */
class Tx_News_Service_CategoryService {

	/**
	 * Get child categories by calling recursive function
	 * and using the caching framework to save some queries
	 *
	 * @param string $idList list of category ids to start
	 * @param integer $counter
	 * @param string $additionalWhere additional where clause
	 * @param boolean $removeGivenIdListFromResult remove the given id list from result
	 * @return string comma separated list of category ids
	 */
	public static function getChildrenCategories($idList, $counter = 0, $additionalWhere = '', $removeGivenIdListFromResult = FALSE) {
		/** @var \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $cache */
		$cache = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\CacheManager')->getCache('cache_news_category');
		$cacheIdentifier = sha1('children' . $idList);

		$entry = $cache->get($cacheIdentifier);
		if (!$entry) {
			$entry = self::getChildrenCategoriesRecursive($idList, $counter, $additionalWhere);
			$cache->set($cacheIdentifier, $entry);
		}

		if ($removeGivenIdListFromResult) {
			$entry = self::removeValuesFromString($entry, $idList);
		}

		return $entry;
	}

	/**
	 * Remove values of a comma separated list from another comma separated list
	 *
	 * @param string $result string comma separated list
	 * @param $toBeRemoved string comma separated list
	 * @return string
	 */
	public static function removeValuesFromString($result, $toBeRemoved) {
		$resultAsArray = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $result, TRUE);
		$idListAsArray = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $toBeRemoved, TRUE);

		$result = implode(',', array_diff($resultAsArray, $idListAsArray));
		return $result;
	}

	/**
	 * Get rootline up by calling recursive function
	 * and using the caching framework to save some queries
	 *
	 * @param integer $id category id to start
	 * @param string $additionalWhere additional where clause
	 * @return string comma separated list of category ids
	 */
	public static function getRootline($id, $additionalWhere = '') {
		$cache = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\CacheManager')->getCache('cache_news_category');
		$cacheIdentifier = sha1('rootline' . $id);

		$entry = $cache->get($cacheIdentifier);
		if (!$entry) {
			$entry = self::getRootlineRecursive($id, $additionalWhere);
			$cache->set($cacheIdentifier, $entry);
		}
		return $entry;
	}

	/**
	 * Get child categories
	 *
	 * @param string $idList list of category ids to start
	 * @param integer $counter
	 * @param string $additionalWhere additional where clause
	 * @return string comma separated list of category ids
	 */
	private static function getChildrenCategoriesRecursive($idList, $counter, $additionalWhere) {
		$result = array();

		// add idlist to the output too
		if ($counter === 0) {
			$result[] = $GLOBALS['TYPO3_DB']->cleanIntList($idList);
		}

		$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
			'uid',
			'sys_category',
			'sys_category.parent IN (' . $GLOBALS['TYPO3_DB']->cleanIntList($idList) . ')
				AND deleted=0 ' . $additionalWhere);

		while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))) {
			$counter++;
			if ($counter > 10000) {
				$GLOBALS['TT']->setTSlogMessage('EXT:news: one or more recursive categories where found');
				return implode(',', $result);
			}
			$subcategories = self::getChildrenCategoriesRecursive($row['uid'], $counter, $additionalWhere);
			$result[] = $row['uid'] . ($subcategories ? ',' . $subcategories : '');
		}
		$GLOBALS['TYPO3_DB']->sql_free_result($res);

		$result = implode(',', $result);
		return $result;
	}

	/**
	 * Get rootline categories
	 *
	 * @param integer $id category id to start
	 * @param integer $counter counter
	 * @param string $additionalWhere additional where clause
	 * @return string comma separated list of category ids
	 */
	public static function getRootlineRecursive($id, $counter = 0, $additionalWhere = '') {
		$id = (int)$id;
		$result = array();

		$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
			'uid,parent',
			'sys_category',
			'uid=' . $id . ' AND deleted=0 ' . $additionalWhere);

		$row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
		$GLOBALS['TYPO3_DB']->sql_free_result($res);
		if ($id === 0 || !$row || $counter > 10000) {
			$GLOBALS['TT']->setTSlogMessage('EXT:news: one or more recursive categories where found');
			return $id;
		}

		$parent = self::getRootlineRecursive($row['parent'], $counter, $additionalWhere);
		$result[] = $row['parent'];
		if ($parent > 0) {
			$result[] = $parent;
		}

		$result = implode(',', $result);
		return $result;
	}


	/**
	 * Translate a category record in the backend
	 *
	 * @param string $default default label
	 * @param array $row category record
	 * @return string
	 * @throws UnexpectedValueException
	 */
	public static function translateCategoryRecord($default, array $row = array()) {
		if (TYPO3_MODE != 'BE') {
			throw new UnexpectedValueException('TYPO3 Mode must be BE');
		}

		$overlayLanguage = (int)$GLOBALS['BE_USER']->uc['newsoverlay'];

		$title = '';

		if ($row['uid'] > 0 && $overlayLanguage > 0 && $row['sys_language_uid'] == 0) {
			$overlayRecord = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
				'*',
				'sys_category',
				'deleted=0 AND sys_language_uid=' . $overlayLanguage . ' AND l10n_parent=' . $row['uid']
			);
			if (isset($overlayRecord[0]['title'])) {
				$title = $overlayRecord[0]['title'] . ' (' . $row['title'] . ')';
			}
		}

		$title = ($title ? $title : $default);

		return $title;
	}

}
